package net.mingsoft.report_data.util;

import net.mingsoft.report_data.constant.ZkDataConstant;
import net.mingsoft.report_data.enums.ReportPeriodType;
import org.joda.time.DateTime;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gengfa
 * @date 22/03/14
 */
public class PeriodUtil {
    private static final String DATETIME_FORMAT = "yyyy-MM-dd 00:00:00";

    private PeriodUtil() {}

    public static Map<String, Object> getDateArgs(ReportPeriodType periodType) {
        HashMap<String, Object> map = new HashMap<>(16);
        String[] dateRange = getDateRange(periodType);
        map.put(ZkDataConstant.KEY_BEGIN_DATE, dateRange[0]);
        map.put(ZkDataConstant.KEY_END_DATE, dateRange[1]);
        return map;
    }

    public static String[] getDateRange(ReportPeriodType periodType) {
        DateTime now = DateTime.now();
        DateTime beginDate = new DateTime();
        DateTime endDate = new DateTime();

        if (periodType == ReportPeriodType.THIS_WEEK) {
            beginDate = now.dayOfWeek().withMinimumValue();
            endDate = now.dayOfWeek().withMaximumValue().plusDays(1);
        } else if (periodType == ReportPeriodType.LAST_WEEK) {
            beginDate = now.minusWeeks(1).dayOfWeek().withMinimumValue();
            endDate = now.minusWeeks(1).dayOfWeek().withMaximumValue().plusDays(1);
        } else if (periodType == ReportPeriodType.THIS_MONTH) {
            beginDate = now.dayOfMonth().withMinimumValue();
            endDate = now.dayOfMonth().withMaximumValue().plusDays(1);
        } else if (periodType == ReportPeriodType.LAST_MONTH) {
            beginDate = now.minusMonths(1).dayOfMonth().withMinimumValue();
            endDate = now.minusMonths(1).dayOfMonth().withMaximumValue().plusDays(1);
        } else if (periodType == ReportPeriodType.THIS_SEASON) {
            DateTime[] season = getSeason(now);
            beginDate = season[0];
            endDate = season[1];
        } else if (periodType == ReportPeriodType.LAST_SEASON) {
            DateTime[] season = getSeason(now.minusMonths(3));
            beginDate = season[0];
            endDate = season[1];
        } else if (periodType == ReportPeriodType.FIRST_HALF_YEAR) {
            DateTime[] halfYear = getHalfYear(now, 1);
            beginDate = halfYear[0];
            endDate = halfYear[1];
        } else if (periodType == ReportPeriodType.LATTER_HALF_LAST_YEAR) {
            DateTime[] halfYear = getHalfYear(now.minusYears(1), 2);
            beginDate = halfYear[0];
            endDate = halfYear[1];
        } else if (periodType == ReportPeriodType.THIS_YEAR) {
            beginDate = now.dayOfYear().withMinimumValue();
            endDate = now.dayOfYear().withMaximumValue().plusDays(1);
        } else if (periodType == ReportPeriodType.LAST_YEAR) {
            beginDate = now.minusYears(1).dayOfYear().withMinimumValue();
            endDate = now.minusYears(1).dayOfYear().withMaximumValue().plusDays(1);
        }
        return new String[]{beginDate.toString(DATETIME_FORMAT), endDate.toString(DATETIME_FORMAT)};
    }

    private static DateTime[] getSeason(DateTime dateTime) {
        DateTime beginDate;
        DateTime endDate;
        final int dayOfMonth = dateTime.getDayOfMonth();
        final int year = dateTime.getYear();
        if (dayOfMonth > 0 && dayOfMonth <= 3) {
            beginDate = new DateTime(year + "-01-01T00:00:00.000+08:00");
            endDate = new DateTime(year + "-04-01T00:00:00.000+08:00");
        } else if (dayOfMonth <= 6) {
            beginDate = new DateTime(year + "-04-01T00:00:00.000+08:00");
            endDate = new DateTime(year + "-07-01T00:00:00.000+08:00");
        } else if (dayOfMonth <= 9) {
            beginDate = new DateTime(year + "-07-01T00:00:00.000+08:00");
            endDate = new DateTime(year + "-10-01T00:00:00.000+08:00");
        } else {
            beginDate = new DateTime(year + "-10-01T00:00:00.000+08:00");
            endDate = new DateTime((year + 1) + "-01-01T00:00:00.000+08:00");
        }
        return new DateTime[]{beginDate, endDate};
    }

    private static DateTime[] getHalfYear(DateTime dateTime, int type) {
        DateTime beginDate;
        DateTime endDate;
        final int year = dateTime.getYear();
        if (type == 1) {
            // 上半年
            beginDate = new DateTime(year + "-01-01T00:00:00.000+08:00");
            endDate = new DateTime(year + "-07-01T00:00:00.000+08:00");
        } else {
            // 下半年
            beginDate = new DateTime(year + "-07-01T00:00:00.000+08:00");
            endDate = new DateTime((year + 1) + "-01-01T00:00:00.000+08:00");
        }
        return new DateTime[]{beginDate, endDate};
    }
}
